package com.cvte.boe.media.views.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.cvte.boe.media.R;
import com.cvte.boe.media.utils.LogUtil;

import java.util.List;
import java.util.Locale;

/**
 * Created by user on 2018/4/8.
 */
public class ComboView extends LinearLayout implements View.OnFocusChangeListener, OnKeyListener {

    //attrs
    private int ap;
    private Context mContext;
    private ImageView imageViewFirst;
    private ImageView imageViewSecond;
    private TextView textView;

    private List<String> list = null;
    private int mCurrentIndex = 0;
    private ComboViewUpdateListener mCallback = null;

    public interface ComboViewUpdateListener {
        boolean onUpdateListener(ComboView view, int index);
    }

    public ComboView(Context context) {
        this(context, null);
    }

    public ComboView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ComboView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ComboView);
        ap = a.getResourceId(R.styleable.ComboView_comboTextAppearance, R.style.comboTextAppearance_Default);
        a.recycle();
        mContext = context;
        init();
    }

    private void init() {
        //self
        setFocusable(true);
        if(getOrientation() == VERTICAL){
            LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
            params.gravity = Gravity.CENTER;
            imageViewFirst = new ImageView(mContext);
            imageViewFirst.setImageResource(R.drawable.comboview_up_arrow);
            imageViewFirst.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageViewFirst.setLayoutParams(params);
            imageViewFirst.setClickable(true);
            addView(imageViewFirst);

            params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
            params.gravity = Gravity.CENTER;
            textView = new TextView(mContext);
            textView.setTextAppearance(mContext, ap);
            textView.setGravity(Gravity.CENTER);
            textView.setLayoutParams(params);
            textView.setTextColor(getResources().getColorStateList(R.color.combo_text_selector));
            textView.setDuplicateParentStateEnabled(true);
            addView(textView);

            params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
            params.gravity = Gravity.CENTER;
            imageViewSecond = new ImageView(mContext);
            imageViewSecond.setImageResource(R.drawable.comboview_down_arrow);
            imageViewSecond.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageViewSecond.setClickable(true);
            imageViewSecond.setLayoutParams(params);
            addView(imageViewSecond);
            imageViewFirst.setVisibility(INVISIBLE);
            imageViewSecond.setVisibility(INVISIBLE);
        }else{
            LayoutParams params = new LayoutParams(0,ViewGroup.LayoutParams.MATCH_PARENT, 1);

            params.gravity = Gravity.CENTER;
            imageViewSecond = new ImageView(mContext);
            imageViewSecond.setImageResource(R.drawable.comboview_left_arrow);
            imageViewSecond.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageViewSecond.setClickable(true);
            imageViewSecond.setLayoutParams(params);
            addView(imageViewSecond);

            params = new LayoutParams(0,ViewGroup.LayoutParams.MATCH_PARENT, 5);
            params.gravity = Gravity.CENTER;
            textView = new TextView(mContext);
            textView.setTextAppearance(mContext, ap);
            textView.setGravity(Gravity.CENTER);
            textView.setLayoutParams(params);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,mContext.getResources().getDimension(R.dimen.text_size_hor_combovide));
            textView.setTextColor(getResources().getColorStateList(R.color.combo_text_selector));
            textView.setDuplicateParentStateEnabled(true);
            addView(textView);

            params = new LayoutParams(0,ViewGroup.LayoutParams.MATCH_PARENT, 1);
            params.gravity = Gravity.CENTER;
            imageViewFirst = new ImageView(mContext);
            imageViewFirst.setImageResource(R.drawable.comboview_right_arrow);
            imageViewFirst.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageViewFirst.setLayoutParams(params);
            imageViewFirst.setClickable(true);
            addView(imageViewFirst);
            imageViewFirst.setVisibility(VISIBLE);
            imageViewSecond.setVisibility(VISIBLE);
        }

        this.setOnFocusChangeListener(this);
        this.setOnKeyListener(this);

        mCurrentIndex = 0;
    }

    public void setCurrentIndex(int index) {
        mCurrentIndex = index;
        if (mCurrentIndex < 0) {
            mCurrentIndex = 0;
        }
        if (list != null) {
            if (mCurrentIndex > list.size() - 1) {
                mCurrentIndex = list.size() - 1;
            }
            textView.setText(list.get(mCurrentIndex));
        } else {
            textView.setText(String.format(Locale.getDefault(), "%d", mCurrentIndex));
        }
    }

    public int getCurrentIndex() {
        return mCurrentIndex;
    }

    public void initList(List<String> list) {
        this.list = list;
    }

    public void setComboViewListener(ComboViewUpdateListener callback) {
        mCallback = callback;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(getOrientation() == HORIZONTAL){
            super.setSelected(hasFocus);
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_ENTER:
                    if (getOrientation() == VERTICAL) {
                        if (isSelected()) {
                            setSelected(false);
                            //doUpdate();
                        } else {
                            setSelected(true);
                        }
                    }
                    return true;
                case KeyEvent.KEYCODE_DPAD_UP:
                case KeyEvent.KEYCODE_DPAD_DOWN:
                    if(isSelected()) {
                        if (getOrientation() == VERTICAL) {
                            onSetValue(keyCode == KeyEvent.KEYCODE_DPAD_UP);
                            return true;
                        }else{
                            return false;
                        }

                    }
                    break;
                case KeyEvent.KEYCODE_DPAD_LEFT:
                case KeyEvent.KEYCODE_DPAD_RIGHT:
                    if(isSelected()) {
                        if (getOrientation() == HORIZONTAL) {
                            onSetValue(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT);
                        }
                        return true;
                    }
                    break;
                case KeyEvent.KEYCODE_MENU:
                    break;
                case KeyEvent.KEYCODE_BACK:
                    break;
                default:
                    if (isSelected()) {
                        return true;
                    }
            }
        }
        return false;
    }

    private boolean doUpdate() {
        if (mCallback != null) {
            return mCallback.onUpdateListener(this, mCurrentIndex);
        }
        return true;
    }

    private Runnable firstImage = new Runnable() {
        @Override
        public void run() {
            imageViewFirst.setPressed(false);
        }
    };

    private Runnable secondImage = new Runnable() {
        @Override
        public void run() {
            imageViewSecond.setPressed(false);
        }
    };

    private void onSetValue(boolean bIncrease) {
        if (bIncrease) {
            imageViewFirst.setPressed(true);
            imageViewFirst.removeCallbacks(firstImage);
            imageViewFirst.postDelayed(firstImage, 200);
        } else {
            imageViewSecond.setPressed(true);
            imageViewSecond.removeCallbacks(secondImage);
            imageViewSecond.postDelayed(secondImage, 200);
        }
        if (list != null) {
            int size = list.size();
            int lastIndex = mCurrentIndex;
            mCurrentIndex = bIncrease ? (mCurrentIndex + 1) % size : (mCurrentIndex - 1 + size) % size;
            if(doUpdate() == true) {
                textView.setText(list.get(mCurrentIndex));
            }else{
                mCurrentIndex = lastIndex;
            }
        } else {
            mCurrentIndex = bIncrease ? mCurrentIndex + 1 : mCurrentIndex - 1;
            if (mCurrentIndex < 0) {
                mCurrentIndex = 0;
            }
            textView.setText(String.format(Locale.getDefault(), "%d", mCurrentIndex));
        }
    }

    @Override
    public void setSelected(boolean selected) {
        if (selected) {
            imageViewFirst.setVisibility(VISIBLE);
            imageViewSecond.setVisibility(VISIBLE);
        } else {
            imageViewFirst.setVisibility(INVISIBLE);
            imageViewSecond.setVisibility(INVISIBLE);
        }
        super.setSelected(selected);
    }
}
